package yl.hs.bmipfull.service.system.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.system.IWHSerEquipmentDao;
import yl.hs.bmipfull.pojo.system.DutyInfoEntity;
import yl.hs.bmipfull.pojo.system.ComEquipmentEntity;
import yl.hs.bmipfull.service.system.IWHSerEquipmentService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;

import java.util.List;
import java.util.Map;

/**
 * 作用:  串口设备信息表服务层接口实现<br/>
 * 创建时间: 2021年06月05日 08时44分57秒 <br/>
 *
 * @author 吴高丰
 */
@Service
public class WHSerEquipmentServiceImpl extends SystemBaseService implements IWHSerEquipmentService {

    @Autowired
    IWHSerEquipmentDao dao;

    @Override
    public List<ComEquipmentEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(ComEquipmentEntity.class);
        return dao.queryList(pageQuery.getQuery());
    }


    @Override
    public ComEquipmentEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional
    public Integer saveWHSerEquipment(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        ComEquipmentEntity entity = new ComEquipmentEntity();
        if (Tl.isEmpty(sn)) {
            //执行保存

            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(ComEquipmentEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入串口设备信息表信息", Tl.toJson(entity), ComEquipmentEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新串口设备信息表信息", Tl.toJson(entity), ComEquipmentEntity.class);
            }
        }

        return result;
    }

    @Override
    @Transactional
    public Integer delete(String[] sn) {
        Integer result = dao.changeStateBySN(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, DutyInfoEntity.class);
        }
        return result;
    }

    @Override
    public List<ComEquipmentEntity> queryListByDBSn(String weighbridgeSn) {
        return dao.queryListByDBSn(weighbridgeSn);
    }

    @Override
    public List<ComEquipmentEntity> queryByListSn(String[] list) {
        return dao.queryByListSn(list);
    }
}